require.config paths: jquery: './jquery-1.7.1.min'
require priority: ['jquery'], [
	'jquery'
	'sammy/sammy-0.7.1.min.js'
	'helper'
	'jquery.couch'
	'search'
	'search_result'
	'donor_detail'
	'add_donor'
	'add_donation'
	'donation_detail'
	'view_receipt'
	'history'
	'jnotifypub'
	'sha1.js'
	'test'
], ($, S, H) ->

	# Define route actions and page changes.
	index = ->
	search = ->
		$('#search_result').mb_search_result(@)
		@change_page 'search'          , 'input:[name="add_donor"]'   , 'add_donor'
		return
	add_donor = ->
		$('#add_donor').mb_add_donor(@)
		@change_page 'add_donor'       , 'input:submit'               , 'donor_detail'
		return
	donor_detail = ->
		$('#history').mb_history(@)
		$('#donor_detail').mb_donor_detail(@)
		@change_page 'donor_detail'    , 'input[name="add_donation"]'  , 'add_donation'
		return
	add_donation = ->
		@change_page 'add_donation'    , 'input:[value="Submit"]'      , 'donation_detail'
		return
	donation_detail = ->
		$('#donation_detail').mb_donation_detail(@)
		@change_page 'donation_detail' , 'input[name="back_to_donor"]' , 'donor_detail'
		return
	view_receipt = ->
		$('#view_receipt').mb_view_receipt(@)
		@change_page 'view_receipt' , null , 'view_receipt'
		return


	# Define a page handler to demonstrate typical behaviour of modules and jQuery plugins.
	test = ->
		# Upon each access of the test route, we will re-apply the test plugin
		# to the test page container. We can pass in the Sammy event context if
		# it is needed within the plugin.
		$('#test').mb_test(@)
		@change_page 'test', 'input[name="test"]', 'index'
		return

	# Define the main application routes
	app = $.sammy '#main', ->

		@get '#/search'          , search
		@get '#/add_donor'       , add_donor
		@get '#/donor_detail'    , donor_detail
		@get '#/add_donation'    , add_donation
		@get '#/donation_detail' , donation_detail
		@get '#/view_receipt' 	, view_receipt
		@get '#/test'            , test
		@get '#/'                , search
		@get '#'                 , search
		@get ''                  , search
		return

	$.sammy '#main', ->
		
		# All route handlers will use the Sammy.Template plugin
		@use 'Template'

		# Define useful app helpers
		@helpers

			# Define a helper to re-locate to another page.  Re-location will occur
			# if a specified trigger element is clicked. A convention is used to
			# notate the page URL and the ID of its main content container, ie,
			# '#/id' selects the page for the web browser and '#id' selects the DOM
			# container element.
			change_page: (cur_id, trigger, new_id) ->
				$(".page:not(##{cur_id})").hide()
				$cur_id = $("##{cur_id}").show()
				if trigger?
					$cur_id.on 'click', trigger, =>
						@app.setLocation "#/#{new_id}"
						return false
				else
					@redirect "#/#{new_id}"
				return

			# Define a helper to build content from a data object using a template
			# located in a jQuery object.
			content: H.content

		# Apply jQuery plugins to their containers
		$('#test').mb_test(@)
		$('#search').mb_search()
		$('#search_result').mb_search_result()
		$('#history').mb_history()
		$('#add_donor').mb_add_donor()
		$('#donor_detail').mb_donor_detail()
		$('#add_donation').mb_add_donation(@)
		$('#donation_detail').mb_donation_detail()
		$('#view_receipt').mb_view_receipt()
		$(@).mb_jnotify()
		$('#view_receipt').mb_view_receipt(@)


	# Run the app when the DOM is ready.history
	$ -> app.run()
	return
